import shutil
import logging
import sys
import os

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)


def read_file_lines(file):
    with open(file, 'r', encoding='utf8', errors='ignore') as f:
        return [l.strip() for l in f.readlines()]


def copy_txt_listed_files(txt_file, dest_dir, src_dir=os.getcwd()):
    if os.path.isfile(txt_file):
        os.makedirs(dest_dir, exist_ok=True)
        for line in read_file_lines(txt_file):
            src_file = os.path.join(src_dir, line.strip())
            if os.path.isfile(src_file):
                logger.info(f'copying {src_file}')
                shutil.copy2(src_file, dest_dir)
            else:
                logger.error(f'{src_file} not found')
    else:
        logger.error(f'{txt_file} not found')


if __name__ == '__main__':
    txt_file = 'files2cp.txt'
    dest_dir = r'C:\Users\LeiYang\Downloads\dst'
    copy_txt_listed_files(txt_file, dest_dir, src_dir=r'C:\G\cs-txt-to-multicol-excel\Txt2MultiColExcelXp\bin\Release')
